﻿using System;
using System.Net;
using System.Net.Mail;
using System.Text;

namespace newBlog.CenterInc.Ctrltor
{
    public class function
    {
        //定义全站通用函数
        public bool Email(string email, string title, string body)
        {
            try
            {
                SmtpClient smtpClient = new SmtpClient();

                //构建发件地址和收件地址
                MailAddress sendAddress = new MailAddress("邮箱账号", "msmk");
                MailAddress receiverAddress = new MailAddress(email);
                //构造一个Email的Message对象 内容信息
                MailMessage message = new MailMessage(sendAddress, receiverAddress);
                message.Subject = title;
                message.SubjectEncoding = Encoding.UTF8;
                message.Body = body;
                message.BodyEncoding = Encoding.UTF8;
                //设置邮件的信息 如登陆密码 账号
                //邮件发送方式  通过网络发送到smtp服务器
                smtpClient.DeliveryMethod = SmtpDeliveryMethod.Network;

                //如果服务器支持安全连接，则将安全连接设为true

                smtpClient.EnableSsl = true;

                smtpClient.UseDefaultCredentials = false;
                smtpClient.Host = "smtp.qq.com";//发件域名
                                                //smtpClient.Port; ;//qq邮箱可以不用端口

                //发件用户登陆信息

                NetworkCredential senderCredential = new NetworkCredential("邮箱账号", "邮箱密码");

                smtpClient.Credentials = senderCredential;

                //发送邮件

                smtpClient.Send(message);
                return false;
            }
            catch
            {
                return true;
            }

        }//发送成功返回false，失败true

        public string CreateRanNum(int Length)
        {
            char[] constant =
           {
        '0','1','2','3','4','5','6','7','8','9',
        'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z',
        'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z'
        };
            StringBuilder newRandom = new StringBuilder(62);
            Random rd = new Random();
            for (int i = 0; i < Length; i++)
            {
                newRandom.Append(constant[rd.Next(62)]);
            }
            return newRandom.ToString();
        }//验证码创建


    }
}